<?php
class Message extends Zend_Db_Table_Abstract {
	
	protected $_primay = "id";
	protected $_name = "message";
	
	const MESSAGE = "message";
	const TYPE = "type";
	const USER_ID = "user_id";
	const CREATED = "created";
	
	protected $_dependentTables = array(
       'UserMessage'
    );
    
    protected $_referenceMap = array (
    'User' => array (
        'columns' => 'user_id', 
        'refTableClass' => 'User', 
        'refColumns' => 'id' 
    ),
    'Type' => array(
        'columns' => 'type',
        'refTableClass' => 'MessageType',
        'refColumns' => 'id'
    ),
    'ParentMessage' => array (
        'columns' => 'parent_message_id', 
        'refTableClass' => 'Message', 
        'refColumns' => 'id' 
    ));
    
    /**
     * Add new message;
     * 
     * @param $data
     * @return message id
     */
    public function addMessage($data) {
    	return $this->insert($data);
    }
    
    /**
     * Find message by id.
     * 
     * @param $id
     */
    public function findById($id) {
    	return $this->find($id)->current();
    }
    
    /**
     * Return user of message.
     * 
     * @param $message
     */
    public static function getUser($message) {
    	return $message->findParentRow("User");
    }
    
    /**
     * Return user message.
     * 
     * @param $message
     */
    public static function getUserMessage($message) {
        $userMessage = $message->findDependentRowset('UserMessage');
        return $userMessage->current();
    }
    
    /**
     * Return target users for give message.
     * 
     * @param $message
     * @param $filter
     */
    public static function getUserMessageTargetUser($message, $filter=true) {
    	$userMessage = new UserMessage();
    	if ($filter) {
    		return $message->findManyToManyRowset('User', 'UserMessage', 'Message', 'TargetUser', $userMessage->select()->where('i.status!=?', 'deleted')->order('created DESC'));
    	} else {
    		return $message->findManyToManyRowset('User', 'UserMessage', 'Message', 'TargetUser');
    	}
    }
    
    /**
     * Get all users by given root message.
     * 1. the given message must be root
     * 2. it include the creator of message as well
     * 
     * @param $message
     * @return return users
     */
    public static function getUsersByRootMessage($message) {
    	$users = array();
    	$index = 0;
    	if (empty($message->parent_message_id)) {
	    	foreach (self::getUserMessageTargetUser($message, false) as $user) {
	    		$users[$index++] = $user;
	    	}
	    	$users[$index++] = self::getUser($message);
    	}
    	return $users;
    }
    
    /**
     * Return parent message by given message.
     * 
     * @param $message
     */
    public static function getParentMessage($message) {
    	return $message->findParentRow("Message", "ParentMessage");
    }
    
    /**
     * Return all reply message to given message.
     * 
     * @param $message
     */
    public static function getReplyMessages($message) {
    	$m = new Message();
    	$where = $m->select()->where("parent_message_id=?", $message->id)
    	->order("created ASC");
    	return $m->fetchAll($where);
    }
    
    /**
     * Get top message by given message id.
     * Top message is the message who has no parent message id.
     * 
     * @param $messageId
     */
    public static function getTopMessage($messageId) {
    	$table = new Message();
        $message = $table->findById($messageId);
    	$parentMessage = self::getParentMessage($message);
    	if (empty($parentMessage)) {
    		return $message;
    	} else {
    		return $parentMessage;
    	}
    }
}
?>